export class BrowserUtils {
    public static setCookie(c_name: string, value: string, expiredays: number = 1): void {

        const tokenStr = window.btoa(value);

        const exDate: Date = new Date();

        exDate.setDate(exDate.getDate() + expiredays);

        document.cookie = `${c_name}= ${tokenStr};expires=${exDate.toUTCString()}`;
    }

    public static getCookie(c_name: string): string {

        let c_start = document.cookie.indexOf(c_name + '=');

        if (c_start > -1) {
            c_start = c_start + c_name.length + 1;
            let c_end = document.cookie.indexOf(';', c_start);

            if (c_end === -1) {
                c_end = document.cookie.length;
            }

            return window.atob(document.cookie.substring(c_start, c_end));
        }

        return '';
    }

    public static deleteCookie(c_name: string): void {

        BrowserUtils.setCookie(c_name, '', -1);
    }

    public static checkToken(): boolean {
        const cookie: string = BrowserUtils.getCookie('token');
        return !!cookie;
    }

    public static backToTarget(target: string): void {
        const origin = location.origin;
        const pathname = location.pathname;

        location.href = `${origin}${pathname}#/${target}`;
    }

}
